﻿using System;
using System.Data;
using System.Data.Objects.DataClasses;

namespace ShippingManage.DataAccessor
{
	public partial class ShippingManageContainer
	{
		partial void OnContextCreated()
		{
			SavingChanges += OnSavingChanges;
		}

		private void OnSavingChanges(object sender, EventArgs e)
		{
			var now = DateTime.Now;
			foreach (var entry in ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified))
			{
				if (!entry.IsRelationship)
				{
					#region modified when create new object

					var timestampedEntity = entry.Entity as IModified;
					if (timestampedEntity != null)
					{
						//assign created
						timestampedEntity.Modified = now;
						timestampedEntity.ModifiedId = Global.CurrentUserId;
						var entity = entry.Entity as EntityObject;
						if (entity != null && entity.EntityState == EntityState.Added)
						{
							timestampedEntity.Created = now;
							timestampedEntity.CreatedId = Global.CurrentUserId;
						}
					}

					#endregion
				}
			}
		}
	}

	public interface IEntity
	{ Guid Id { get; set; } }

	public interface ICreated
	{
		DateTime Created { get; set; }
		Guid CreatedId { get; set; }
	}

	public interface IModified : ICreated
	{
		DateTime Modified { get; set; }
		Guid ModifiedId { get; set; }
	}

	public partial class HouseAirWayBill : IModified
	{ }

	public partial class VienDongShipping : IModified
	{
		#region ServiceType

		public ServiceTypes ServiceTypeEnum
		{
			get { return (ServiceTypes)ServiceType; }
			set
			{
				ServiceType = (short)value;
			}
		}

		#endregion

	}

	public partial class Customer : IModified
	{ }

	public enum ServiceTypes
	{
		DoorToDoor = 2,
		AirportToAirport = 1,
		Other = 0,
		Unknown = -1
	}
}
